package com.estar.assistsurvy.activity;

import java.util.ArrayList;

import com.estar.app.appsrvyspds.domain.ReturnSubVideoSrvyVO;
import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.MainPageViewpagerAdapter;
import com.estar.assistsurvy.entity.ScalePagerItem;
import com.estar.assistsurvy.fragment.CarLeftFragment;
import com.estar.assistsurvy.fragment.CarRightFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;

public class DemagedPositionActivity extends BaseActivity implements OnClickListener,OnCheckedChangeListener,OnPageChangeListener{

	private LinearLayout returnBack;
	private ImageView goPhoto;
	private RadioButton carLeft,carRight;
	private ViewPager positionPager;
	private ArrayList<Fragment> fragments;
	private FragmentManager manager;
	private CarLeftFragment leftFragment;
	private CarRightFragment rightFragment;
	private MainPageViewpagerAdapter adapter;
	private ArrayList<ScalePagerItem> list,defaultList;
	private Intent intent;
	private ReturnSubVideoSrvyVO returnSubVideoSrvyVo;
	private int[] imgs=new int[]{R.drawable.pic_drivecard,R.drawable.pic_idcard01,R.drawable.pic_idcard02,R.drawable.pic_bankcard,R.drawable.pic_car14,R.drawable.pic_car15,R.drawable.pic_car16};
	private String[] titles=new String[]{"","","","","","",""};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_demage_position);
		initView();
	}
	
	private void initView(){
		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);
		positionPager=find(R.id.positionPager);
		carLeft=find(R.id.carLeft);
		carLeft.setOnCheckedChangeListener(this);
		carRight=find(R.id.carRight);
		carRight.setOnCheckedChangeListener(this);
		goPhoto=find(R.id.goPhoto);
		goPhoto.setOnClickListener(this);
		
		fragments=new ArrayList<>();
		leftFragment=new CarLeftFragment();
		fragments.add(leftFragment);
		rightFragment=new CarRightFragment();
		fragments.add(rightFragment);
		manager=getSupportFragmentManager();
		adapter=new MainPageViewpagerAdapter(manager, fragments);
		positionPager.setAdapter(adapter);
		positionPager.addOnPageChangeListener(this);
		
		intent=getIntent();
		returnSubVideoSrvyVo = (ReturnSubVideoSrvyVO) intent.getSerializableExtra("returnSubVideoSrvyVO");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			finish();
			break;
			
		case R.id.goPhoto:
			list=(ArrayList<ScalePagerItem>) leftFragment.getList().clone();
			list.addAll(rightFragment.getList());
			defaultList=new ArrayList<>();
			for(int i=0;i<imgs.length;i++){
				ScalePagerItem item=new ScalePagerItem();
				item.setTitles(titles[i]);
				item.setScaleImages(imgs[i]);
				defaultList.add(item);
			}
			if(list.size()>0){
				list.addAll(defaultList);
				if(returnSubVideoSrvyVo==null){
					showShortToast("未获取到当前按件信息，请退出重试!");
					return;
				}
				intent=new Intent(DemagedPositionActivity.this, TakePictureActivity.class);
				intent.putExtra("list", list);
				intent.putExtra("returnSubVideoSrvyVO",returnSubVideoSrvyVo);
				startActivity(intent);
				finish();
			}else{
				showShortToast("请选择至少一个受损部位!");
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		switch (position) {
		case 0:
			clearState();
			carLeft.setChecked(true);
			carLeft.setTextColor(getResources().getColor(R.color.colorBgWhite));
			
			break;

		case 1:
			clearState();
			carRight.setChecked(true);
			carRight.setTextColor(getResources().getColor(R.color.colorBgWhite));
			
			break;

		default:
			break;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if(isChecked){
			switch (buttonView.getId()) {

			case R.id.carLeft:
				clearState();
				positionPager.setCurrentItem(0);
				carLeft.setChecked(true);
				carLeft.setTextColor(getResources().getColor(R.color.colorBgWhite));
				
				break;

			case R.id.carRight:
				clearState();
				positionPager.setCurrentItem(1);
				carRight.setChecked(true);
				carRight.setTextColor(getResources().getColor(R.color.colorBgWhite));
				
				break;

			default:
				break;
			}
		}
	}
	
	private void clearState(){
		carLeft.setChecked(false);
		carLeft.setTextColor(getResources().getColor(R.color.colorTextOrange));
		carRight.setChecked(false);
		carRight.setTextColor(getResources().getColor(R.color.colorTextOrange));
	}
}
